package com.zxbit.flash.presenter;

import android.location.Location;

import com.zxbit.flash.BuildConfig;
import com.zxbit.flash.base.BasePresenter;
import com.zxbit.flash.common.UserInfo;
import com.zxbit.flash.mvpview.RegisterMvpView;
import com.zxbit.flash.network.CustomConsumer;
import com.zxbit.flash.network.ParamsFactory;
import com.zxbit.flash.response.BlankResponse;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.functions.Consumer;

public class RegisterPresenter extends BasePresenter {
    private RegisterMvpView mvpView;

    public RegisterPresenter(RegisterMvpView mvpView) {
        this.mvpView = mvpView;
    }

    public void registerRequest(String userName, String passWord, String checkCode, String type) {
        Map<String, Object> formMap = new HashMap<String, Object>();
        formMap.put("userName", userName);
        formMap.put("passWord", passWord);
        formMap.put("checkCode", checkCode);
        formMap.put("type", type); // type 1 注册  2找回密码
        toSubScribe(req.doRegisterQuest(ParamsFactory.getCommonParams(formMap)), new CustomConsumer<BlankResponse>() {
            @Override
            public void call(BlankResponse response) {
                mvpView.onGetDataNext(response);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }

    public void uploadLocation(Location location, final String userPhone) {
        Map<String, Object> map = new HashMap<>();
        map.put("longitude", location.getLongitude());
        map.put("latitude", location.getLatitude());
        map.put("saveType", "1");
        map.put("userPhone", userPhone);
        toSubScribe(req.uploadLocationRegister(BuildConfig.channel, UserInfo.getToken(), ParamsFactory.getCommonParams(map)), new CustomConsumer<BlankResponse>() {
            @Override
            public void call(BlankResponse response) {
                mvpView.onGetDataNext(userPhone);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }

}
